package pmr.entry;

import java.lang.reflect.Method;

import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;

import pmr.core.Template;
import pmr.utils.SCUtils;

public class EntryPoint implements IEditorActionDelegate {

	private final Template template = new Template();

	public void setActiveEditor(IAction action, IEditorPart targetEditor) {
		if (targetEditor == null)
			return;
		IEditorInput editorInput = targetEditor.getEditorInput();

		Class<?> c = editorInput.getClass();
		try {
			Method method = c.getMethod("getFile");
			IFile file = (IFile) method.invoke(editorInput);
			ICompilationUnit compilationUnit = SCUtils.getJavaElement(file);
			template.setUnit(compilationUnit);
			template.setProject(compilationUnit.getJavaProject());

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void run(IAction action) {
		template.findUnusedMethods(action);
	}

	public void selectionChanged(IAction action, ISelection selection) {
		template.setSelection(selection);
	}

}
